package com.lzw.mall.admin.controller;


import com.lzw.mall.admin.util.Constants;
import com.lzw.mall.common.entity.Category;
import com.lzw.mall.common.entity.Product;
import com.lzw.mall.common.service.ICategoryService;
import com.lzw.mall.common.vo.LayuiTableVO;
import com.lzw.mall.common.service.IProductService;
import com.lzw.mall.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-22
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/index")
    public String page(){
        return "product";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(@RequestParam Integer page, @RequestParam Integer limit,@RequestParam(required = false,name = "keyword") String keyword,String status){
        return productService.list(page, limit, keyword,status);
    }

    @ResponseBody
    @PostMapping("/save")
    public ResultVO save(Product p) {

        return productService.doSave(p);
    }
    @PostMapping("/doUp")
    @ResponseBody
    public void doUp(@RequestParam("ids[]") Integer[] ids ){
        productService.doUp(ids);
    }
    @PostMapping("/doDown")
    @ResponseBody
    public void doDown(@RequestParam("ids[]") Integer[] ids ){
        productService.doDown(ids);
    }

    @ResponseBody
    @PostMapping("/delete")
    public void delete(Integer id) {
        productService.doDelete(id);
    }

    @ResponseBody
    @PostMapping("/avatar")
    public ResultVO avatar(MultipartFile file){
        ResultVO vo=new ResultVO();
        String fileName=System.currentTimeMillis() + ".png";
        File f=new File("D:\\test01\\product\\"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,String> u = new HashMap();
        u.put("avatar", Constants.UPLOAD_PATH+"product\\"+fileName);
        vo.setCode(0);
        vo.setData(u);
        return vo;
    }




    @GetMapping("/{id}")
    @ResponseBody
    public Map<String,Object> selectById(@PathVariable("id") Integer id){
        Map<String, Object> map = new HashMap<>();
        Product p=productService.getById(id);
        Category c = categoryService.getById(p.getCategoryId());//三级分类
        Category c1 = categoryService.getById(c.getParentId());//二级分类

        //所有的三级分类
        List<Category> list3=categoryService.selectByParentId(c.getParentId());
        //所有的二级分类
        List<Category> list2=categoryService.selectByParentId(c1.getParentId());
        //所有的一级分类
        List<Category> list1=categoryService.selectByParentId(0);
        map.put("product",p);
        map.put("list1",list1);
        map.put("list2",list2);
        map.put("list3",list3);
        map.put("topCategory",c1.getParentId());
        map.put("lastCategory",c.getParentId());
        return map;
    }
}
